<?php

declare(strict_types=1);

namespace App\CustHub\Controller;

use App\CustHub\Annotation\ApiAuth;
use App\CustHub\Annotation\VerifySignature;
use App\CustHub\Request\UserRequest;
use App\CustHub\Service\AppService;
use App\CustHub\Service\ChUserService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\PostMapping;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;

#[Controller('ch/user'),ApiAuth,VerifySignature]
class UserController extends BaseController
{
    #[Inject]
    protected ChUserService $service;

    #[Inject]
    protected AppService $appService;


    /**
     * 获取用户信息
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('getUserInfo')]
    public function getUserInfo(): \Psr\Http\Message\ResponseInterface
    {
        return $this->success('用户信息',$this->service->getUserInfo($this->request->all()));
    }

    /**
     * 获取用户基本信息
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('getInfo')]
    public function getInfo(): \Psr\Http\Message\ResponseInterface
    {
        return $this->success('用户基本信息',$this->service->getInfo((int)$this->request->input('uid')));
    }

    /**
     * 获取功能指引
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('getGuide')]
    public function getGuide(): \Psr\Http\Message\ResponseInterface
    {
        return $this->success('功能指引',$this->service->guide((int)$this->request->input('uid')));
    }

    /**
     * 获取用户下拉列表
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('getSelectList')]
    public function getSelectList(): \Psr\Http\Message\ResponseInterface
    {
        return $this->success($this->service->getSelectList($this->request->all()));
    }

    /**
     * 发送验证码
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     * @throws \RedisException
     */
    #[PostMapping('sendSms')]
    public function sendSms(): \Psr\Http\Message\ResponseInterface
    {
        return $this->service->sendCode($this->request->input('main_uid')) ? $this->success() : $this->error();
    }

    /**
     * 修改密码
     * @param UserRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('modifyPassword')]
    public function modifyPassword(UserRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->service->modifyPassword($request->validated()) ? $this->success() : $this->error();
    }

    /**
     * 获取接口信息
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('getApiInfo')]
    public function getApiInfo(): ResponseInterface
    {
        return $this->success($this->appService->getApiInfo($this->request->all()));
    }

    /**
     * 重置app_id
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('resetAppId')]
    public function resetAppId(): ResponseInterface
    {
        return $this->success(['app_id'=>$this->appService->resetAppId($this->request->all())]);
    }

    /**
     * 重置app_secret
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('resetAppSecret')]
    public function resetAppSecret(): ResponseInterface
    {
        return $this->success(['app_secret'=>$this->appService->resetAppSecret($this->request->all())]);
    }


    /**
     * 回调通知配置
     * @param UserRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('saveNotifyConfig')]
    public function saveNotifyConfig(UserRequest $request): ResponseInterface
    {
        return $this->appService->saveNotifyConfig($request->all()) ? $this->success() : $this->error();
    }

}